package com.crm.system.controller.activityManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.qo.activityManager.CustomerActivityAddQO;
import com.crm.model.qo.activityManager.CustomerProductActivityQO;
import com.crm.model.vo.activityManager.CustomerContractProductActivityVO;
import com.crm.model.vo.activityManager.CustomerProductActivityUpdateVO;
import com.crm.model.vo.activityManager.CustomerProductActivityVO;
import com.crm.model.vo.activityManager.CustomerRecentActivityVO;
import com.crm.system.service.activityManager.CustomerProductActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 活动管理
 * @author luojie
 * @date 2021/2/23 11:26
 */
@RestController
@RequestMapping("/customerProductActivity")
@Slf4j
public class CustomerProductActivityController {
    @Autowired
    private CustomerProductActivityService customerProductActivityService;

    /**
     * 活动管理--添加活动
     * @param request
     * @param customerActivityAddQO
     * @return
     */
    @PostMapping("/addProductActivity")
    public DataResponse addProductActivity(HttpServletRequest request, @RequestBody CustomerActivityAddQO customerActivityAddQO){
        try{
            int result = customerProductActivityService.addProductActivity(customerActivityAddQO);
            if(result == 1){
                //活动重复
                return new DhResponse(ResponseCodeEnum.PRODUCT_ACTIVITY_EXISTS.getResponseCode());
            }else if(result == 0){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }else{
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
        }catch(Exception e){
            log.error("添加活动 == customerProductActivity addProductActivity error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 活动管理--活动列表
     * @author yincuimei
     * @return
     */
    @PostMapping("/selectCustomerProductActivityList")
    public DataResponse selectCustomerProductActivityList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                          @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                          @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                          @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc)
                                                          CustomerProductActivityQO productActivityQO//查询条件
    ){
        try{
            DhPageInfo<CustomerProductActivityVO> customerProductVOList = customerProductActivityService.selectCustomerProductActivityVOList(productActivityQO,pageIndex,pageSize,sortName,sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerProductVOList);
        }catch(Exception e){
            log.error("活动列表 == customerProductActivity selectCustomerProductActivityList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 活动列表--(活动查看)根据活动ID查询活动及产品信息
     * @param productActivityId ---活动ID
     * @author yincuimei
     * @return
     */
    @PostMapping("/selectCustomerProductActivityById")
    public DataResponse selectCustomerProductActivityById(@RequestParam(value="productActivityId")String productActivityId//活动ID
    ){
        try{
            CustomerProductActivityUpdateVO customerProductActivityUpdateVO = customerProductActivityService.selectCustomerProductActivityById(productActivityId);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerProductActivityUpdateVO);
        }catch(Exception e){
            log.error("活动查看 == customerProductActivity selectCustomerProductActivityById error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     *活动列表--(活动上架)根据活动ID查询活动是否在有效期内
     * @author yincuimei
     * @return
     */
    @PostMapping("/selectActivityIsInValidityPeriod")
    public DataResponse selectActivityIsInValidityPeriod(@RequestParam(value="productActivityId")String productActivityId){//活动ID
        try{
            Boolean result = customerProductActivityService.selectActivityIsInValidityPeriod(productActivityId);
            if(result){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }catch(Exception e){
            log.error("活动上架 == customerProductActivity selectActivityIsInValidityPeriod error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     *活动列表--上/下架操作(活动有效期内的可以上架;活动下架没有限制)
     * @author yincuimei
     * @return
     */
    @PostMapping("/updateActivityShelfStatus")
    public DataResponse updateActivityShelfStatus(@RequestParam(value="productActivityId")String productActivityId,//活动ID
                                        @RequestParam(value="activityShelfStatus",required = false)String activityShelfStatus//活动状态--0:上架;1:下架
    ){
        try{
            Boolean result = customerProductActivityService.updateActivityShelfStatus(productActivityId,activityShelfStatus);
            if(result){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }catch(Exception e){
            log.error("活动上/下架 == customerProductActivity updateActivityShelfStatus error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 活动列表--修改活动
     * @param request
     * @param customerActivityAddQO
     * @return
     */
    @PostMapping("/editProductActivity")
    public DataResponse editProductActivity(HttpServletRequest request, @RequestBody CustomerActivityAddQO customerActivityAddQO){
        try{
            int result = customerProductActivityService.editProductActivity(customerActivityAddQO);
            if(result == 1){
                //活动重复
                return new DhResponse(ResponseCodeEnum.PRODUCT_ACTIVITY_EXISTS.getResponseCode());
            }else if(result == 0){
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            }else{
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
        }catch(Exception e){
            log.error("编辑活动 == customerProductActivity editProductActivity error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 新建合同--获取有效期范围内活动产品列表
     * @author yincuimei
     * @return
     */
    @GetMapping("/selectValidityPeriodCustomerActivityList")
    public DataResponse selectValidityPeriodCustomerActivityList(@RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                          @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                          @RequestParam(value = "searchInfo", required = false) String searchInfo//查询条件
    ){
        try{
            DhPageInfo<CustomerContractProductActivityVO> customerProductVOList = customerProductActivityService.selectValidityPeriodCustomerActivityList(pageIndex,pageSize,searchInfo);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerProductVOList);
        }catch(Exception e){
            log.error("活动列表 == customerProductActivity selectCustomerProductActivityList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /***
     * @Description:任务管理-任务列表 查询近期活动列表
     * @Author: luojie
     * @Date: 2021/4/26 16:45
     */
    @GetMapping("/selectRecentActivityList")
    public DataResponse selectRecentActivityList(HttpServletRequest request){
            DhPageInfo<CustomerRecentActivityVO> customerProductVOList = customerProductActivityService.selectRecentActivityList();
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerProductVOList);
    }




/*    @GetMapping("/selectRecentActivityList")
    public DataResponse selectRecentActivityList(){
            DhPageInfo<CustomerProductActivityVO> customerProductVOList = customerProductActivityService.selectRecentActivityList();
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),customerProductVOList);
    }*/


}
